﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebUI.Helper;



namespace WebUI.Con.Admin
{
    public class AdminController : Controller
    {
        #region 默认页面index，含有Admin管理的模板页面
        /// <summary>
        /// 默认页面
        /// </summary>
        /// <returns></returns>
        
        public ActionResult Index()
        {
            return View();
        }
        #endregion
        #region 列表用户，返回部分视图，加载userDB强类型
        /// <summary>
        /// 列表用户，返回部分视图，加载userDB强类型
        /// </summary>
        /// <returns></returns>
        [Common.Attributes.Skip]

        public ActionResult AdminUserList()
        {
            var model = OperateContext.Current.BLLSession.IUserDBBLL.GetListBy(s => s.UserID > 0).Select(s => s.ToPOCO()).ToList();
            return PartialView(model);
            //return OperateContext.Current.RedirectAjax("UserList", "用户列表", PartialView(model), "");
        }
        #endregion
        #region 将商品信息显示到下拉列表框，是个部分视图，含有GoodsSort强类型
        [Common.Attributes.Skip]

        public ActionResult AdminGoods()
        {
            //var GoodsSorts = new Model.ViewModel.GoodsSort();

            List<Model.ViewModel.GoodsSort> GoodsSorts = new List<Model.ViewModel.GoodsSort>();

            var MGoodsSorts = OperateContext.Current.BLLSession.IGoodsSortBLL.GetListBy(s => s.GoodsSortId > 0).ToList();
            foreach (var g in MGoodsSorts)
            {
                var GoodsSort = new Model.ViewModel.GoodsSort();
                GoodsSort.GoodsSortID = g.GoodsSortId;
                GoodsSort.GoodsSortName = g.GoodsSortName;
                GoodsSort.Goods = g.Goods.Select(s => new SelectListItem
                {

                    Text = s.GoodsName,
                    Value = s.GoodsID.ToString()
                });
                GoodsSorts.Add(GoodsSort);
            }




            // return Json(GoodsSorts, JsonRequestBehavior.AllowGet);
            return PartialView(GoodsSorts);
        }

        #endregion
        #region 响应下拉商品大类列表框，点击返回以目前GoodsSortID的商品列表部分视图，含 goods的强类型
        //响应下拉框
        [Common.Attributes.Skip]

        public ActionResult GoodssByTeamID(int id)
        {
            IEnumerable<Model.ViewModel.Goods> Goods_List = new List<Model.ViewModel.Goods>();
            var GS = OperateContext.Current.BLLSession.IGoodBLL.GetListBy(s => s.GoodsSortID == id).ToList();
            ViewData["GoodsSortName"] = OperateContext.Current.BLLSession.IGoodsSortBLL.GetListBy(s => s.GoodsSortId == id).Select(s => s.GoodsSortName).FirstOrDefault().ToString();
            // var data = new ViewDataDictionary();
            //data.Add("GoodsSortName", OperateContext.Current.BLLSession.IGoodsSortBLL.GetListBy(s => s.GoodsSortId == id).Select(s => s.GoodsSortName).FirstOrDefault().ToString());
            ViewData["GoodsSortID"] = id;

            Goods_List = GS.Select(p => new Model.ViewModel.Goods()
            {
                // GoodsSortName=p.GoodsSort.GoodsSortName,                         
                GoodsName = p.GoodsName,
                GoodsID = p.GoodsID,
                MeasureUnite = p.MeasureUnite,
                Quntity = p.Quntity ?? 0,
                StockQuntity = p.StockQuntity ?? 0,
                StockUnite = p.StockUnite,
                Multiple = p.Multiple ?? 0,
                SalePrice = p.SalePrice ?? 0,
                PurchasePrice = p.PurchasePrice ?? 0


            });
            return PartialView(Goods_List);
        }
        #endregion
        #region 费用项目操作
        /// <summary>
        /// 查找----返回CostClass强类型部分视图
        /// </summary>
        /// <returns></returns>
        [Common.Attributes.Skip]

        public ActionResult M_CostClass()
        {
            //IEnumerable<Model.CostClass> CostClass_List = new List<Model.CostClass>();
            var GS = OperateContext.Current.BLLSession.ICostClassBLL.GetListBy(s => s.CostClassID > 0).ToList();
            return PartialView(GS);
        }
        /// <summary>
        /// 增加--费用项目
        /// </summary>
        /// <returns></returns>
        [Common.Attributes.Skip]

        public ActionResult Btn_AddCostClass()
        {
            if (Request["CostClassName"] != null && Request["CostClassName"].ToString() != "")
            {
                Model.CostClass Add = new Model.CostClass() { CostClassName = Request["CostClassName"].ToString() };

                OperateContext.Current.BLLSession.ICostClassBLL.Add(Add);
                return Content("ok");
            }

            else
            {
                return Content("Error");
            }

        }
        /// <summary>
        /// 修改--
        /// </summary>
        /// <returns></returns>
        [Common.Attributes.Skip]
        public ActionResult Btn_ModifyCostClass()
        {
            if (Request["CostClassID"] != null && Request["CostClassID"].ToString() != "")
            {

                Model.CostClass Modify_CostClass = new Model.CostClass();
                Modify_CostClass.CostClassID = Convert.ToInt32(Request["CostClassID"]);
                Modify_CostClass.CostClassName = Request["CostClassName"].ToString();

                // var properties =Edit_good.GetType().GetProperties();

                OperateContext.Current.BLLSession.ICostClassBLL.Modify(Modify_CostClass, new string[] { "CostClassName" });
                return Content("ok");
            }

            else
            {
                return Content("Error");
            }

        }

        //删除----费用科目
        [Common.Attributes.Skip]
        public ActionResult Btn_DeleteCostClass()
        {

            if (Request["CostClassID"] != null && Request["CostClassID"].ToString() != "")
            {
                //Model.Good Add = new Model.Good() { GoodsName = Request["name"].ToString(), GoodsSortID = Convert.ToInt32(Request["GoosSortID"]) };
                Model.CostClass Delete_CostClass = new Model.CostClass();
                Delete_CostClass.CostClassID = Convert.ToInt32(Request["CostClassID"]);


                OperateContext.Current.BLLSession.ICostClassBLL.Del(Delete_CostClass);
                return Content("ok");
            }

            else
            {
                return Content("Error");
            }
        }
        #endregion
        #region CridetSetup项目操作
        /// <summary>
        /// 查找----返回CridetSetup强类型部分视图
        /// </summary>
        /// <returns></returns>
        [Common.Attributes.Skip]

        public ActionResult CreditSetup_List()
        {
            //IEnumerable<Model.CostClass> CostClass_List = new List<Model.CostClass>();
            var GS = OperateContext.Current.BLLSession.ICreditSetupBLL.GetListBy(s => s.CreditID > 0).ToList();
            return PartialView(GS);
        }
        /// <summary>
        /// 增加--CridetSetup项目
        /// </summary>
        /// <returns></returns>
        [Common.Attributes.Skip]

        public ActionResult Btn_AddCreditSetup()
        {
            if (Request["CreditSetupName"] != null && Request["CreditSetupName"].ToString() != "")
            {
                Model.CreditSetup Add = new Model.CreditSetup() { CreditName = Request["CreditSetupName"].ToString(),IsUsed=false };

                OperateContext.Current.BLLSession.ICreditSetupBLL.Add(Add);
                return Content("ok");
            }

            else
            {
                return Content("Error");
            }

        }
        /// <summary>
        /// 修改--CreditSetup
        /// </summary>
        /// <returns></returns>
        [Common.Attributes.Skip]
        public ActionResult Btn_ModifyCreditSetup()
        {
            if (Request["CreditSetupID"] != null && Request["CreditSetupID"].ToString() != "")
            {

                Model.CreditSetup Modify_CostClass = new Model.CreditSetup();
                Modify_CostClass.CreditID= Convert.ToInt32(Request["CreditSetupID"]);
                Modify_CostClass.CreditName= Request["CreditSetupName"].ToString();

                // var properties =Edit_good.GetType().GetProperties();

                OperateContext.Current.BLLSession.ICreditSetupBLL.Modify(Modify_CostClass, new string[] { "CreditName" });
                return Content("ok");
            }

            else
            {
                return Content("Error");
            }

        }

        //删除----CreditSetup
        [Common.Attributes.Skip]
        public ActionResult Btn_DeleteCreditSetup()
        {

            if (Request["CreditSetupID"] != null && Request["CreditSetupID"].ToString() != "")
            {
                //Model.Good Add = new Model.Good() { GoodsName = Request["name"].ToString(), GoodsSortID = Convert.ToInt32(Request["GoosSortID"]) };
                Model.CreditSetup Delete_CostClass = new Model.CreditSetup();
                Delete_CostClass.CreditID = Convert.ToInt32(Request["CreditSetupID"]);


                OperateContext.Current.BLLSession.ICreditSetupBLL.Del(Delete_CostClass);
                return Content("ok");
            }

            else
            {
                return Content("Error");
            }
        }
        #endregion
        #region Good,GoodsSort操作
        [Common.Attributes.Skip]
        public ActionResult btn_AddGordsSort()
        {
            if (Request["name"] != null && Request["name"].ToString() != "")
            {
                Model.GoodsSort Add = new Model.GoodsSort() { GoodsSortName = Request["name"].ToString() };

                OperateContext.Current.BLLSession.IGoodsSortBLL.Add(Add);
                return Content("ok");
            }

            else
            {
                return Content("Error");
            }

        }
        /// <summary>
        /// 根据传入的分类GoodsSortID添加新的具体商品
        /// </summary>
        /// <returns></returns>
        [Common.Attributes.Skip]

        public ActionResult btn_AddGords()
        {

            if (Request["name"] != null && Request["name"].ToString() != "")
            {
                Model.Good Add = new Model.Good() { GoodsName = Request["name"].ToString(), GoodsSortID = Convert.ToInt32(Request["GoosSortID"]) };

                OperateContext.Current.BLLSession.IGoodBLL.Add(Add);
                return Content("ok");
            }

            else
            {
                return Content("Error");
            }
        }
        /// <summary>
        /// 根据传入Goods修改商品 
        /// </summary>
        /// <returns></returns>
        [Common.Attributes.Skip]
        public ActionResult btn_EditGoods()
        {

            if (Request["GoodsName"] != null && Request["GoodsName"].ToString() != "")
            {
                //Model.Good Add = new Model.Good() { GoodsName = Request["name"].ToString(), GoodsSortID = Convert.ToInt32(Request["GoosSortID"]) };
                Model.Good Edit_good = new Model.Good();
                Edit_good.GoodsID = Convert.ToInt32(Request["GoodsID"]);
                Edit_good.GoodsName = Request["GoodsName"].ToString();
                Edit_good.Quntity = Convert.ToInt32(Request["Quntity"]);
                Edit_good.SalePrice = Convert.ToDecimal(Request["SalePrice"]);
                Edit_good.PurchasePrice = Convert.ToDecimal(Request["PurchasePrice"]);
                Edit_good.MeasureUnite = Request["MeasureUnite"].ToString();
                Edit_good.StockUnite = Request["StockUnite"].ToString();
                Edit_good.Multiple = Convert.ToInt32(Request["Multiple"]);
                // var properties =Edit_good.GetType().GetProperties();

                OperateContext.Current.BLLSession.IGoodBLL.Modify(Edit_good, new string[] { "GoodsName", "Quntity", "SalePrice", "PurchasePrice", "MeasureUnite", "Multiple", "StockUnite" });
                return Content("ok");
            }

            else
            {
                return Content("Error");
            }
        }

        /// <summary>
        /// 根据传入GoodsSortID修改商品大类名 +btn_ModifyGoodsSortName
        /// </summary>
        /// <returns></returns>
        [Common.Attributes.Skip]
        public ActionResult btn_ModifyGoodsSortName()
        {

            if (Request["GoodsSortID"] != null && Request["GoodsSortID"].ToString() != "")
            {
                //Model.Good Add = new Model.Good() { GoodsName = Request["name"].ToString(), GoodsSortID = Convert.ToInt32(Request["GoosSortID"]) };
                Model.GoodsSort Modify_GoodsSortName = new Model.GoodsSort();
                Modify_GoodsSortName.GoodsSortId = Convert.ToInt32(Request["GoodsSortID"]);
                Modify_GoodsSortName.GoodsSortName = Request["GoodsSortName"].ToString();

                OperateContext.Current.BLLSession.IGoodsSortBLL.Modify(Modify_GoodsSortName, new string[] { "GoodsSortName" });
                return Content("ok");
            }

            else
            {
                return Content("Error");
            }
        }
        /// <summary>
        /// 根据传入GoodsSortID修改商品大类名 +btn_ModifyGoodsSortName
        /// </summary>
        /// <returns></returns>
        [Common.Attributes.Skip]
        public ActionResult btn_DeleteGoods()
        {

            if (Request["GoodsID"] != null && Request["GoodsID"].ToString() != "")
            {
                //Model.Good Add = new Model.Good() { GoodsName = Request["name"].ToString(), GoodsSortID = Convert.ToInt32(Request["GoosSortID"]) };
                Model.Good Delete_Goods = new Model.Good();
                Delete_Goods.GoodsID = Convert.ToInt32(Request["GoodsID"]);


                OperateContext.Current.BLLSession.IGoodBLL.Del(Delete_Goods);
                return Content("ok");
            }

            else
            {
                return Content("Error");
            }
        }
        /// <summary>
        /// 根据传入GoodsSortID修改商品大类名 +btn_ModifyGoodsSortName
        /// </summary>
        /// <returns></returns>
        [Common.Attributes.Skip]
        public ActionResult btn_DeleteGoodsSort()
        {

            if (Request["GoodsSortID"] != null && Request["GoodsSortID"].ToString() != "")
            {
                //Model.Good Add = new Model.Good() { GoodsName = Request["name"].ToString(), GoodsSortID = Convert.ToInt32(Request["GoosSortID"]) };
                Model.GoodsSort Delete_GoodsSort = new Model.GoodsSort();
                Delete_GoodsSort.GoodsSortId = Convert.ToInt32(Request["GoodsSortID"]);

                if (OperateContext.Current.BLLSession.IGoodBLL.GetListBy(s => s.GoodsSortID == Delete_GoodsSort.GoodsSortId).ToList().Count == 0)
                {
                    OperateContext.Current.BLLSession.IGoodsSortBLL.Del(Delete_GoodsSort);
                    return Content("ok");
                }
                else { return Content("Has"); }
            }

            else
            {
                return Content("Error");
            }
        }

        [Common.Attributes.Skip]
        /// <summary>
        /// 添加货物上架数量
        /// </summary>
        /// <returns></returns>
        public ActionResult btn_UpGoods()
        {
            if (Request["GoodsID"] != null && Request["GoodsID"].ToString() != "")
            {
                Model.Good g_M = new Model.Good();
                g_M.GoodsID = Convert.ToInt32(Request["GoodsID"]);

                g_M.Quntity = OperateContext.Current.BLLSession.IGoodBLL.GetListBy_NoTrack(s => s.GoodsID == g_M.GoodsID).Select(s => s.Quntity).FirstOrDefault() + Convert.ToInt32(Request["Quntity"]);

               int p= OperateContext.Current.BLLSession.IGoodBLL.Modify(g_M, new string[] { "Quntity" });
               if (p == 1)
               {
                   return Content("ok");
               }
            }
            return Content("Error");
        }
        #endregion
        
    }
}
